<?php 
	jimport('joomla.application.component.model');
	class sale_tsc_che_bienModelsu_luoc_che_bien extends JModel
	{
	   //cap nhat so luong
	   function cap_nhat_lai_so_luong($id_pro,$id_wh,$num)
        {
            
            $db = &JFactory::getDBO();
            $sql = "select * from #__erp_warehouse_ex_pro 
            where 
            id_product = '".$id_pro."' 
            and id_warehouse = '".$id_wh."'
            and number_of > 0
            ORDER BY date_in
            ";
			$db->setQuery($sql);
            $data = $db->loadObjectList();	
            foreach($data as $row)
            {
                $num = $row->number_of+$num;
                    $sql = "UPDATE #__erp_warehouse_ex_pro SET
                                number_of = '".$num."'
                                WHERE 
                                id ='".$row->id."'";
                                $db->setQuery($sql);
                                $db->query();
                    return;
            }    
        }	   
        
	   //ds sp theo don hang
       function ds_sp_bill($id_bill)
        {$db = &JFactory::getDBO();
            $sql = "SELECT ep.name as pro_name, od.number, od.status, od.id as id_od, ep.id as id_pro
                    FROM #__erp_sale_order od, #__erp_product ep
                    WHERE id_order = '".$id_bill."'
                    AND ep.id = od.id_product
                ";
            $db->setQuery($sql);
            //print_r($db->loadObjectList());die;
            return $db->loadObjectList();  
        }
	   //ds don hang
        function ds_don_hang($date=null)
        {
            
            //neu date = null thi lay ngay hien hanh
            if($date == null)
            {
                $date = date('Y-m-d');
            }
            //dinh dang lai $date giong voi CSDL
            else
            {
                $d = substr($date,3,2);
                $m = substr($date,0,2);
                $y = substr($date,6,4);
                $date = $y."-".$m."-".$d;
                
            }
            $db = &JFactory::getDBO();
            
                /*$sql = "SELECT sa.*, so.id_product, so.number, so.status as sp_st 
                FROM #__erp_sale sa, #__erp_sale_order so 
                WHERE date LIKE '$date%' 
                AND sa.id = so.id_order 
                AND sa.del = 0 
                AND sa.status = 1 
                ORDER BY sa.date ASC";*/
                $sql = "SELECT *
                    FROM #__erp_sale
                    WHERE date LIKE '$date%' 
                    AND del = 0
                    AND status = 1 
                    ORDER BY date 
                ";
            $db->setQuery($sql);
            //print_r($db);
            $data = $db->loadObjectList();
            //print_r($data);
            return $db->loadObjectList();
            
        }
	}
?>